<?php if(!defined('WDK_PATH')) exit('No direct script access allowed');
require_once WDK_PATH.'core/WdkClass.php';
require_once WDK_PATH.'core/WdkController.php';
require_once WDK_PATH.'core/WdkPlugin.php';
require_once WDK_PATH.'core/WdkModel.php';
require_once WDK_PATH.'core/WdkManager.php';
require_once WDK_PATH.'exceptions/Exception.php';
require_once WDK_PATH.'tpu/firephp/fb.php';
require_once WDK_PATH.'tpu/tpl_/Template_.class.php';
class Wdk
{
	private static $helpers = array();
	public static function helper($name='')
	{
		$name = ucfirst($name);
		$cname = strtolower($name);
		if(isset(self::$helpers[$cname]) == false)
		{
			//	1. check own project helper path.
			$file = '';
			if(defined('PROJECT_PATH'))
			{
				$file = PROJECT_PATH.'classes/_helpers/'.$name.'/Class.php';				
			}
			if(file_exists($file) == false)
			{
				//	2. check ADK helper path.
				$file = WDK_PATH.'helpers/'.$name.'/Class.php';
				if(file_exists($file) == false)
				{
					throw new ClassFileNotExistException(sprintf("Helper[%s] does not exist.", ucfirst($name)));
				}
			}
			require_once $file;
			$hname = "Hp_".$name;
			self::$helpers[$cname] = new $hname;
		}
		return self::$helpers[$cname];
	}
	
	private static $classes = array();
	public static function get($name='')
	{
		$name = ucfirst($name);
		$cname = strtolower($name);
		$hname = "Cl_".$name;
		if(isset(self::$classes[$cname]) == false)
		{
			$file = PROJECT_PATH.'classes/'.$name.'/Class.php';
			if(strtolower(substr($name, -7)) == 'manager')
			{
				$file = PROJECT_PATH.'classes/'.substr($name, 0,-7).'/Manager.php';
			}
			if(file_exists($file) == false)
			{
				throw new ClassFileNotExistException(sprintf("class[%s] does not exist.", ucfirst($name)));
			}
			require_once $file;
			self::$classes[$cname] = new $hname;
		}
		return new $hname;
	}
	
	private static $models = array();
	public static function model($name='')
	{
		$name = ucfirst($name);
		$cname = strtolower($name);
		if(isset(self::$models[$cname]) == false)
		{
			//	2. check ADK helper path.
			$file = PROJECT_PATH.'classes/'.$name.'/Model.php';
			if(file_exists($file) == false)
			{
				throw new ClassFileNotExistException(sprintf("model[%s] does not exist.", ucfirst($name)));
			}
			require_once $file;
			$hname = "Md_".$name;
			self::$models[$cname] = new $hname;
		}
		return self::$models[$cname];
	}
	
	private static $dbo = null;
	public static function dbo()
	{
		if(self::$dbo == null)
		{
			require_once WDK_PATH.'core/WdkDbo.php';
			self::$dbo = new WdkDbo();
		}
		return self::$dbo; 
	}
	
	private static $plugins = array();
	public static function plugin($name='', $param=array())
	{
		if(isset(Wdk::$plugins[$name]) == FALSE)
		{
			require_once PLGPATH."{$name}/plugin.php";
			$className = "Plugin{$name}";
			Wdk::$plugins[$name] = new $className();
		}
		Wdk::$plugins[$name]->__init($param);
		return Wdk::$plugins[$name];
	}
	
	private static $config = array();
	public static function config()
	{
		if(empty(Wdk::$config) == true)
		{
			require_once BASE_PATH.'config.php';
			Wdk::$config = $config;
		}
		return Wdk::$config;
	}
}


function utRc($i=1)
{
	if($i == 0) return '';
	$idx = array(65, 97);
	return utRc(--$i).chr($idx[rand(0,1)]+rand(0, 25));
}
function utRand()
{
	if(func_num_args() == 1 && is_array(func_get_arg(0)))
	{
		$args=func_get_arg(0);
	}
	else
	{
		$args=func_get_args();
	}
	return $args[rand(0,count($args)-1)];
}